home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Miscellaneous / gdbm / gdbmseq.c < prev    next >
Text File  |  1992-04-05  |  4KB  |  138 lines

  1. /* gdbmseq.c - Routines to visit all keys.  Not in sorted order. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.        
  28. *************************************************************************/
  29.  
  30.  
  31. #include "gdbmdefs.h"
  32.  
  33. /* Special extern for this file. */
  34. extern char *_gdbm_read_entry ();
  35.  
  36.  
  37. /* Find and read the next entry in the hash structure for DBF starting
  38.    at ELEM_LOC of the current bucket and using RETURN_VAL as the place to
  39.    put the data that is found. */
  40.  
  41. static void
  42. get_next_key (dbf, elem_loc, return_val)
  43.      gdbm_file_info *dbf;
  44.      int elem_loc;
  45.      datum *return_val;
  46. {
  47.   int   found;            /* Have we found the next key. */
  48.   char  *find_data;        /* Data pointer returned by find_key. */
  49.  
  50.   /* Find the next key. */
  51.   found = FALSE;
  52.   while (!found)
  53.     {
  54.       /* Advance to the next location in the bucket. */
  55.       elem_loc++;
  56.       if (elem_loc == dbf->header->bucket_elems)
  57.     {
  58.       /* We have finished the current bucket, get the next bucket.  */
  59.       elem_loc = 0;
  60.  
  61.       /* Find the next bucket.  It is possible several entries in
  62.          the bucket directory point to the same bucket. */
  63.       while (dbf->bucket_dir < dbf->header->dir_size / sizeof (int)
  64.          && dbf->cache_entry->ca_adr == dbf->dir[dbf->bucket_dir])
  65.         dbf->bucket_dir++;
  66.  
  67.       /* Check to see if there was a next bucket. */
  68.       if (dbf->bucket_dir < dbf->header->dir_size / sizeof (int))
  69.         _gdbm_get_bucket (dbf, dbf->bucket_dir);          
  70.       else
  71.         /* No next key, just return. */
  72.         return ;
  73.     }
  74.       found = dbf->bucket->h_table[elem_loc].hash_value != -1;
  75.     }
  76.   
  77.   /* Found the next key, read it into return_val. */
  78.   find_data = _gdbm_read_entry (dbf, elem_loc);
  79.   return_val->dsize = dbf->bucket->h_table[elem_loc].key_size;
  80.   if (return_val->dsize == 0)
  81.     return_val->dptr = (char *) malloc (1);
  82.   else
  83.     return_val->dptr = (char *) malloc (return_val->dsize);
  84.   if (return_val->dptr == NULL) _gdbm_fatal (dbf, "malloc error");
  85.   bcopy (find_data, return_val->dptr, return_val->dsize);
  86. }
  87.  
  88.  
  89. /* Start the visit of all keys in the database.  This produces something in
  90.    hash order, not in any sorted order.  */
  91.  
  92. datum
  93. gdbm_firstkey (dbf)
  94.      gdbm_file_info *dbf;
  95. {
  96.   datum return_val;        /* To return the first key. */
  97.  
  98.   /* Set the default return value for not finding a first entry. */
  99.   return_val.dptr = NULL;
  100.  
  101.   /* Get the first bucket.  */
  102.   _gdbm_get_bucket (dbf, 0);
  103.  
  104.   /* Look for first entry. */
  105.   get_next_key (dbf, -1, &return_val);
  106.  
  107.   return return_val;
  108. }
  109.  
  110.  
  111. /* Continue visiting all keys.  The next key following KEY is returned. */
  112.  
  113. datum
  114. gdbm_nextkey (dbf, key)
  115.      gdbm_file_info *dbf;
  116.      datum key;
  117. {
  118.   datum  return_val;        /* The return value. */
  119.   int    elem_loc;        /* The location in the bucket. */
  120.   char  *find_data;        /* Data pointer returned by _gdbm_findkey. */
  121.   long     hash_val;        /* Returned by _gdbm_findkey. */
  122.  
  123.   /* Set the default return value for no next entry. */
  124.   return_val.dptr = NULL;
  125.  
  126.   /* Do we have a valid key? */
  127.   if (key.dptr == NULL) return return_val;
  128.  
  129.   /* Find the key.  */
  130.   elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val);
  131.   if (elem_loc == -1) return return_val;
  132.  
  133.   /* Find the next key. */  
  134.   get_next_key (dbf, elem_loc, &return_val);
  135.  
  136.   return return_val;
  137. }
  138.